package com.sykj.why.controller.front.successorProject;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.SuccessorProjectT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.SuccessorProjectTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/7/2
 * @desc 传承项目前端控制访问层
 */

@RestController
@RequestMapping("/front/successor/project")
@Slf4j
@Api(value = "/front/successor/project",description = "传承项目前端控制访问层")
public class FrontSuccessorAlbumController {

    @Autowired
    private SuccessorProjectTService successorProjectTService;

    /**
     * 查询该传承人下的传承项目
     * @param successorId
     * @return
     */
    @RequestMapping(value = "/findBySuccessor/{successorId}",method = RequestMethod.GET)
    @ApiOperation(value = "/findBySuccessor/{successorId}",notes = "查询该传承人下的传承项目")
    public ResponseEntity findBySuccessor(@PathVariable String successorId){
        try {
            return Response.success("查询成功", successorProjectTService.findListBySuccessor(successorId));
        }catch (WhyException e){
            return Response.serverError(e.getMessage(), e.getCode());
        }catch (Exception e){
            log.error("查询该传承人下的传承项目出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 申请传承项目
     * @param successorProjectT
     * @return
     */
    @RequestMapping(value = "/apply",method = RequestMethod.POST)
    @ApiOperation(value = "/apply",notes = "申请传承项目")
    public ResponseEntity apply(@RequestBody SuccessorProjectT successorProjectT){
        try {
            successorProjectTService.saveSuccessorProject(successorProjectT);
            return Response.success("申请成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("申请传承项目出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改传承项目
     * @param successorProjectT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改传承项目")
    public ResponseEntity update(@RequestBody SuccessorProjectT successorProjectT){
        try {
            successorProjectTService.updateSuccessorProject(successorProjectT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改传承项目出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
